<template>
  <el-container class="home_container">
    <!-- Header -->
    <el-header>
      <div>
        <img height="10%" width="10%" src="../assets/logo.png" alt />
        <span>用户管理系统</span>
      </div>
      <el-button type="info" @click="logout">Logout</el-button>
    </el-header>
    <!-- Container -->
    <el-container>
      <!-- Aside left 侧边栏 侧边 -->
      <el-aside :width="isCollapse ? '64px':'200px' ">
        <!-- 折叠与展开 -->
        <div class="toggle-button" @click="toggleCollapse" :collapse="isCollapse">折叠/展开</div>
        <el-menu
          unique-opened
          background-color= "#333744"
          text-color= "#fff"
          active-text-color= "#409bff"
          :collapse= "isCollapse"
          :collapse-transition= "false"
          :default-active= "activePath"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for=" item in menulist " :key="item.id">
            <template slot="title">
              <em :class="iconsObj[item.id]"></em>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/'+subItem.path)"
            >
              <template slot="title">
                <em class="el-icon-menu"></em>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- container Right 主体内容 -->
      <el-container>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      // 将菜单项与图标对应
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 双向绑定 控制是否展开
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      // 清空token
      window.sessionStorage.clear()
      // 重新回到login页面
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res)
    },
    // 点击按钮切换菜单 折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 将高亮菜单保存 避免刷新失效
    saveNavState(activePath) {
      console.log(activePath)
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 20px;
  align-items: center;
  color: #ffffff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #373d49;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
  text-align: center;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  color: #ffffff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
